package com.hilaver.dzmis.service.impl;

import java.util.List;

import com.hilaver.dzmis.Constants;
import com.hilaver.dzmis.basicinfo.BiReference;
import com.hilaver.dzmis.codetable.CtMachineType;
import com.hilaver.dzmis.service.AbstractBaseService;
import com.hilaver.dzmis.service.IEntityService;
import com.hilaver.dzmis.util.SimpleObj2XML;

public class BiReferenceServiceImpl extends AbstractBaseService implements
		IEntityService {

	private CtServiceImpl ctService;
	
	public BiReferenceServiceImpl(){
		this.ctService = new CtServiceImpl();
	}
	@Override
	public String delete(int id) throws Exception {
		return super.delete(BiReference.class.getName(), id);
	}
	
	public String edit(BiReference reference) throws Exception {
		if (reference.getId() != null) {
			BiReference old = (BiReference) this.dao.get(
					BiReference.class.getName(), reference.getId());
			
			if(reference.getFrontPhoto() == null){
				reference.setFrontPhoto(old.getFrontPhoto());
				reference.setFrontPhotoName(old.getFrontPhotoName());
			}
			
			if(reference.getBackPhoto() == null){
				reference.setBackPhoto(old.getBackPhoto());
				reference.setBackPhotoName(old.getBackPhotoName());
			}
			
			if(reference.getOtherPhoto() == null){
				reference.setOtherPhoto(old.getOtherPhoto());
				reference.setOtherPhotoName(old.getOtherPhotoName());
			}
			
			this.dao.getSession().evict(old);
		}
		this.dao.saveOrUpdate(reference);
		return "{success: true}";
	}

	@Override
	public String get(int id) throws Exception {
		return super.get(BiReference.class.getName(), id);
	}

	@Override
	public String getAllPagination(int page, String sort, String order)
			throws Exception {
		return super.getAllPagination(BiReference.class.getName(), page, sort, order);
	}
	
	public String getAllPagination(int page, String sort, String order, String[] filters)
			throws Exception {
		int offset = Constants.PAGE_SIZE * (page - 1);
		List<BiReference> objList = this.dao.getAllPagination(BiReference.class
				.getName(), offset, Constants.PAGE_SIZE, sort, order, filters);
		StringBuffer xmlSB = new StringBuffer();
		xmlSB.append(Constants.XML_DOC_DEFINE);
		xmlSB.append(Constants.XML_ROOT_TAG[0]);
		for (BiReference obj : objList) {
			xmlSB.append(Constants.XML_ITEM_TAG[0]);
			xmlSB.append(Constants.XML_INDEX_TAG[0]);
			xmlSB.append(++offset);
			xmlSB.append(Constants.XML_INDEX_TAG[1]);

			xmlSB.append(this.toXMLFragment(obj));

			xmlSB.append(Constants.XML_ITEM_TAG[1]);
		}

		xmlSB.append(Constants.XML_CURRENT_PAGE_TAG[0]);
		xmlSB.append(page);
		xmlSB.append(Constants.XML_CURRENT_PAGE_TAG[1]);

		xmlSB.append(Constants.XML_ITEM_TOTAL_TAG[0]);
		xmlSB.append(this.dao.getCount(BiReference.class.getName(),
				filters));
		xmlSB.append(Constants.XML_ITEM_TOTAL_TAG[1]);
		xmlSB.append(Constants.XML_ROOT_TAG[1]);
		return xmlSB.toString();
	}

	public String getAll(String sort, String order) throws Exception{
		return super.getAll(BiReference.class.getName(), sort, order);
	}
	
	public BiReference getObj(int id) throws Exception {
		return (BiReference)super.getObj(BiReference.class.getName(), id);
	}
	
	private Object toXMLFragment(BiReference obj) throws Exception {
		String rtn = SimpleObj2XML.toXMLFragment(obj);
		rtn = rtn.substring(0, rtn.indexOf("<ctMachineType>")
				+ "<ctMachineType>".length())
				+ this.ctService.getDescription(CtMachineType.class.getName(), obj.getCtMachineType(), locale) +rtn.substring(rtn.indexOf("</ctMachineType>"));
		rtn += "<ctMachineTypeCode>" + obj.getCtMachineType() + "</ctMachineTypeCode>";
		if(obj.getFrontPhoto() != null){
			writeFile(realFileDir + obj.getFrontPhotoName(), obj.getFrontPhoto());
		}
		if(obj.getBackPhoto() != null){
			writeFile(realFileDir + obj.getBackPhotoName(), obj.getBackPhoto());
		}
		if(obj.getOtherPhoto() != null){
			writeFile(realFileDir + obj.getOtherPhotoName(), obj.getOtherPhoto());
		}
		return rtn;
	}
}
